package com.smt.take.ui.fragment

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.smt.take.R
import com.smt.take.ui.activity.LoginActivity
import com.smt.take.utils.TakeOutApp
import org.jetbrains.anko.find

class UserFragment : Fragment() {

    private lateinit var userInfo: LinearLayout
    private lateinit var username: TextView
    private lateinit var phoneNUm: TextView
    private lateinit var login: ImageView

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val view: View = View.inflate(activity, R.layout.fragment_user, null)
        login = view.find(R.id.login)
        userInfo = view.find(R.id.ll_userinfo)
        username = view.find(R.id.username)
        phoneNUm = view.find(R.id.phone)
        login.setOnClickListener {
            startActivity(Intent(context, LoginActivity::class.java))
        }
        return view
    }

    @SuppressLint("SetTextI18n")
    override fun onStart() {
        super.onStart()
        // 展示登陆成功后的UI效果
        val user = TakeOutApp.sUser
        if (user.id == -1) {
            // 未登陆
            userInfo.visibility = View.GONE
            login.visibility = View.VISIBLE
        } else {
            // 已登陆
            userInfo.visibility = View.VISIBLE
            login.visibility = View.GONE
            username.text = "欢迎您,${user.name}"
            phoneNUm.text = user.phone
        }
    }
}